package com.ybq.library.controller;

/**
 * @author : Ybq777
 * @version : 1.0.0
 * @description :
 * @createTime : 2024/12/2 16:55
 */

import com.ybq.library.entity.YbqUser;
import com.ybq.library.service.YbqUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class YbqUserController {

    @Autowired
    private YbqUserService userService;

    // 新增用户信息接口
    /*
    * http://localhost:8080/users
    {
    "YbqUserName": "Ybq",
    "YbqPassword": "123456",
    "YbqUserPermission": 1
       }
    * */
    @PostMapping
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public ResponseEntity<String> addUser(@RequestBody YbqUser user) {
        System.out.println(user);
        try {
            userService.addUser(user);
            return ResponseEntity.ok("User added successfully");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to add user: " + e.getMessage());
        }
    }
    // 删除用户信息接口，需验证权限
    //http://localhost:8080/users/1
    @DeleteMapping("/{userId}")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public void deleteUserById(@PathVariable("userId") String userId) {
        userService.deleteUserById(userId);
    }

    // 修改用户信息接口，需验证权限
    /*
    * http://localhost:8080/users
    *
    *     {
        "YbqId" : 2,
    "YbqUserName": "Ybq",
    "YbqPassword": "1234567",
    "YbqUserPermission": 1
    }
    * */
    @PutMapping
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public void updateUser(@RequestBody YbqUser user) {
        userService.updateUser(user);
    }

    // 查询用户信息接口
    //http://localhost:8080/users/single-condition?condition=YbqUserId&value=2
    @GetMapping("/multiple-conditions")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public List<YbqUser> getUserBySingleCondition(@RequestParam("username") String username,
                                         @RequestParam("permission") Object permission) {
        return userService.getUserBySingleCondition(username ,permission);
    }
    // 查询用户信息接口
    @GetMapping("/single-condition")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public YbqUser getUserBySingleCondition1(@RequestParam("condition") String condition,
                                         @RequestParam("value") Object value) {
        return userService.getUserBySingleCondition1(condition, value);
    }
    // 查询所有用户信息接口
    //http://localhost:8080/users/all-users
    @GetMapping("/all-users")
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    public List<YbqUser> getAllUsers() {
        return userService.getAllUsers();
    }
}